package com.briup.web.controller;

import com.briup.bean.Shop;
import com.briup.bean.User;
import com.briup.service.ICollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class CollectController {

    @Autowired
    private ICollectService collectService;

    @GetMapping(value = {"/toCollect"})
    public String toCollect(HttpSession session, Model model){
        User user = (User) session.getAttribute("user");
        List<Shop> shopList = collectService.findUserAllCollect(user.getId());
        model.addAttribute("collects",shopList);
        return "collect";
    }

    @GetMapping(value = {"/collect","/addCollect"})
    @ResponseBody
    public String addShopToCollect(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        Long shopId = Long.valueOf(request.getParameter("shopId"));
        collectService.addCollect(user,shopId);
        return "success";
    }

    @GetMapping(value = "/delCollect")
    @ResponseBody
    public String delCollect(Long collectId,HttpSession session){
        User user = (User) session.getAttribute("user");
        collectService.deleteCollect(user.getId(),collectId);
        return "success";
    }

    @GetMapping(value = "/findCollect")
    @ResponseBody
    public String findCollect(Long shopId,HttpSession session){
        User user = (User) session.getAttribute("user");
        if(collectService.findCollect(user.getId(),shopId)){
            return "Collected"; //已经被收藏了！
        }
        return null;
    }
}
